package com.googlecode.monitoring.webpages.dao;


import com.googlecode.monitoring.dao.AgentConfigurationDao;
import com.googlecode.monitoring.exception.DataDaoException;
import com.googlecode.monitoring.model.AlertText;
import com.googlecode.monitoring.model.ItemToCheck;
import com.googlecode.monitoring.model.Person;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.Resource;
import org.springframework.stereotype.Repository;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import java.util.ArrayList;
import java.util.List;

/**
 * Author: Sergiy Doroshenko
 * Date: Aug 30, 2010
 * Time: 2:19:38 PM
 */
@Repository
public class WebPagesAgentConfigurationDao extends AgentConfigurationDao {

    private Resource xmlWebPageConfigResource;
    private WebPagesAgentXmlUtils xmlUtils = new WebPagesAgentXmlUtils();
    Logger log = Logger.getLogger(WebPagesAgentConfigurationDao.class);
    private List<Person> commonResponsiblePersonList;

    public Resource getXmlWebPageConfigResource() {
        return xmlWebPageConfigResource;
    }

    @Autowired
    public void setXmlWebPageConfigResource(Resource xmlWebPageConfigResource) {
        this.xmlWebPageConfigResource = xmlWebPageConfigResource;
    }

    @Override
    public List<ItemToCheck> getAllItemsFromConfiguration() {
        try {
            Document doc = xmlUtils.getDocumentFromResource(xmlWebPageConfigResource, true);
            NodeList nodes = doc.getElementsByTagName("item");
            List<ItemToCheck> items = new ArrayList<ItemToCheck>(nodes.getLength());
            log.debug("readed " + nodes.getLength() + " nodes item");
            for (int i = 0; i < nodes.getLength(); i++) {
                Node xmlItem = nodes.item(i);
                if (xmlItem.getNodeType() == Node.ELEMENT_NODE) {
                    try {
                        items.add(xmlUtils.buildDataToCheck((Element) xmlItem));
                    } catch (Exception e) {
                        log.error(xmlItem.toString() + ";i=" + i, e);
                        throw new DataDaoException(xmlItem.toString(), e);
                    }
                }
            }
            return items;
        } catch (Throwable e) {
            throw new DataDaoException(e);
        }
    }

//    @Override
//    public String replacePlaseHoldersInAlertText(Person person, ItemToCheck item, String text) throws DataDaoException {
//        try {
//            Document doc = xmlUtils.getDocumentFromResource(xmlWebPagesConfigResource, true);
//            List<String[]> placeHolders = xmlUtils.getPlaceHolders(text);
//            for (String[] p : placeHolders) {
//                if (p[0].equals("person")) {
//                    text = xmlUtils.replacePlaceHoldersInPerson(person, text, doc, p[1]);
//                } else if (p[0].equals("item")) {
//                    text = xmlUtils.replacePlaceHoldersInItem(item, text, doc, p);
//                }
//            }
//        } catch (Throwable e) {
//            throw new DataDaoException(e);
//        }
//        return text;
//    }


    @Override
    public AlertText getAlertText() throws DataDaoException {
        try {
            Document doc = xmlUtils.getDocumentFromResource(xmlWebPageConfigResource, false);
            return xmlUtils.getAlertText(doc);
        } catch (Throwable e) {
            log.error(e);
            throw new DataDaoException(e);
        }
    }

    @Override
    public List<Person> getCommonResponsiblePersons() {
        if (commonResponsiblePersonList == null) {
            commonResponsiblePersonList = new ArrayList<Person>();
            try {
                Document doc = xmlUtils.getDocumentFromResource(xmlWebPageConfigResource, true);
                Element alertsElem = (Element) doc.getElementsByTagName("alerts").item(0);
                NodeList personNodes = alertsElem.getElementsByTagName("person");
                commonResponsiblePersonList.addAll(xmlUtils.buildPersons(personNodes));
            } catch (Throwable e) {
                log.error(e);
                throw new DataDaoException(e);
            }
        }
        return commonResponsiblePersonList;
    }
}
